// =====================================================================
//
//  This file is part of the Microsoft Dynamics CRM SDK code samples.
//
//  Copyright (C) Microsoft Corporation.  All rights reserved.
//
//  This source code is intended only as a supplement to Microsoft
//  Development Tools and/or on-line documentation.  See these other
//  materials for detailed information regarding Microsoft code samples.
//
//  THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
//  KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
//  IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
//  PARTICULAR PURPOSE.
//
// =====================================================================
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace CRMCustom
{
	public partial class MessagePropertyNameForm : Form
	{
		private CrmMessage _message;
		private RadioButton[] _buttonList;

		public MessagePropertyNameForm(CrmMessage message)
		{
			if (null == message)
			{
				throw new ArgumentNullException("message");
			}

			InitializeComponent();

			this._message = message;

			//Create the list
			this._buttonList = new RadioButton[message.ImageMessagePropertyNames.Count];

			//Add the radio buttons to the list
			int radioSpace = radTemplateItem.Margin.Top + radTemplateItem.Height;
			int radioTop = radTemplateItem.Top + radioSpace;
			for (int i = 0; i < message.ImageMessagePropertyNames.Count; i++)
			{
				RadioButton button;
				if (0 == i)
				{
					button = radTemplateItem;
				}
				else
				{
					button = new RadioButton();
					button.Left = radTemplateItem.Left;
					button.Top = radioTop;
					grpMessageProperties.Controls.Add(button);

					radioTop += radioSpace;
				}

				ImageMessagePropertyName property = message.ImageMessagePropertyNames[i];
				button.Text = property.Label;
				button.Tag = property;
				button.CheckedChanged += new EventHandler(ButtonCheckedChanged);

				this._buttonList[i] = button;
			}

			radTemplateItem.Visible = true;
			radTemplateItem.Checked = true;
		}

		public ImageMessagePropertyName SelectedMessagePropertyName
		{
			get
			{
				if (0 == this._buttonList.Length)
				{
					return null;
				}

				for (int i = 0; i < this._buttonList.Length; i++)
				{
					if (this._buttonList[i].Checked)
					{
						return (ImageMessagePropertyName)this._buttonList[i].Tag;
					}
				}

				return null;
			}
		}

		private void btnOK_Click(object sender, EventArgs e)
		{
			if (null == this.SelectedMessagePropertyName)
			{
				MessageBox.Show("No Message Property has been checked. Check at least one or click Cancel",
					"Message Properties", MessageBoxButtons.OK, MessageBoxIcon.Warning);
				this.DialogResult = DialogResult.None;
				return;
			}

			this.DialogResult = DialogResult.OK;
		}

		private void ButtonCheckedChanged(object sender, EventArgs e)
		{
			RadioButton button = sender as RadioButton;
			if (null == button)
			{
				txtDescription.Clear();
			}
			else
			{
				txtDescription.Text = ((ImageMessagePropertyName)button.Tag).Description;
			}
		}
	}
}
